﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SZHL.UI.AppCode;
using System.Reflection;
using FastReflectionLib;
using SZHL.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data;

namespace SZHL.UI.API
{
    public class CQQDManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(CQQDManage).GetMethod(msg.Action.ToUpper());
            CQQDManage model = new CQQDManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }


        //考勤签到
        public void ADDCQQD(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_CQQD cc = new ZH_CQQD();
            cc.CRDate = DateTime.Now;

            var kq = new JH_Auth_KQB().GetEntity(d => d.Type == "1");

            if (P1 == "1")//签到
            {
                cc.CRType = "签到";
                if (kq != null && !string.IsNullOrEmpty(kq.StartTime))
                {
                    var date = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " " + kq.StartTime + ":00");
                    if (date < cc.CRDate)
                    {
                        cc.CRType = "迟到";
                    }
                }
            }
            else if (P1 == "2") //签退
            {
                cc.CRType = "签退";
                if (kq != null && !string.IsNullOrEmpty(kq.EndTime))
                {
                    var date = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " " + kq.EndTime + ":00");
                    if (date > cc.CRDate)
                    {
                        cc.CRType = "早退";
                    }
                }
            }

            cc.CRUserName = UserInfo.User.UserRealName;

            cc.CRUser = UserInfo.User.UserName;

            new ZH_CQQDB().Insert(cc);

            msg.Result = cc.CRDate.Value.ToString("HH:mm:ss");
        }

        public void GETCQQDSTATUS(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string cuser = UserInfo.User.UserName;

            string SQL = "select top 1 convert(char(8),CRDate,108) as CRDate from ZH_CQQD where CRUser='" + cuser + "' and (CRType='签到' or CRType='迟到') and DATEDIFF(DAY,CRDate,GETDATE())<1 order by CRDate ";
            object CRDate1 = new ZH_CQQDB().ExsSclarSql(SQL);

            SQL = "select top 1 convert(char(8),CRDate,108) as CRDate from ZH_CQQD where CRUser='" + cuser + "' and (CRType='签退' or CRType='早退') and DATEDIFF(DAY,CRDate,GETDATE())<1 order by CRDate desc ";
            object CRDate2 = new ZH_CQQDB().ExsSclarSql(SQL);


            msg.Result = new JObject(new JProperty("CRDate1", CRDate1 == null ? "" : CRDate1), new JProperty("CRDate2", CRDate2 == null ? "" : CRDate2));

        }
        public void GETCQQDLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = "";
            string branchCode = context.Request.QueryString["b"] ?? "";
            if (branchCode != "")
            {
                string strQueryUser = context.Request["QUSER"] ?? "";
                if (strQueryUser == "")
                {
                    strQueryUser = new JH_Auth_BranchB().GetAllUserByBranch(branchCode);
                }
                strWhere += string.Format("  CRUser in ('{0}')", strQueryUser.ToFormatLike());
            }
            else {
                strWhere += string.Format("  CRUser in ('{0}')", UserInfo.User.UserName);
            }
            if (P2 != "")  //月份
            {
                strWhere += string.Format(" and convert(char(7),CRDate,120)='{0}' ", P1);
            }

            string start = context.Request["start"];
            string end = context.Request["end"];
            if (!string.IsNullOrEmpty(start))
            {
                strWhere += string.Format(" and [CRDate]>='{0}' ", start);
            }
            if (!string.IsNullOrEmpty(end))
            {
                strWhere += string.Format(" and [CRDate]<='{0}' ", end);
            }
            int page = 0;
            int.TryParse(context.Request.QueryString["p"] ?? "1", out page);
            page = page == 0 ? 1 : page;
            int total = 0;
            DataTable dtList = new ZH_XTGGB().GetDataPager("ZH_CQQD", "* ", 8, page, "CRDate desc", strWhere, ref total);



            msg.Result = dtList;
            msg.Result1 = total;

        }
        public void GETCQQDLIST_BY(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = " 1=1 ";

            string strQueryUser = context.Request["QUSER"] ?? "";
            if (strQueryUser != "")
            {
                if (strQueryUser.IndexOf(',') != -1)
                {
                    strQueryUser = "0";
                }
                strWhere += string.Format(" And  CRUser in('{0}')", strQueryUser.ToFormatLike());
            }
            if (P2 != "")  //月份
            {
                strWhere += string.Format(" and convert(char(7),CRDate,120)='{0}' ", P1);
            }

            string start = context.Request["start"];
            string end = context.Request["end"];
            if (!string.IsNullOrEmpty(start))
            {
                strWhere += string.Format(" and [CRDate]>='{0}' ", start);
            }
            if (!string.IsNullOrEmpty(end))
            {
                strWhere += string.Format(" and [CRDate]<='{0}' ", end);
            }

            DataTable dt = new ZH_CQQDB().GetDTByCommand("select CRType+' '+convert(char(10),CRDate,108) as title, CRDate as start,CASE CRType WHEN '早退'  THEN 'red' WHEN '迟到' THEN 'red' ELSE 'green' END as color from ZH_CQQD where " + strWhere + " order by CRDate ");
            msg.Result = dt;

        }


    }
}